<template>
  <div id="user-login-page">
    <div class="login-container">
      <div class="login-header">
        <h1>触图万象</h1>
        <div class="title">企业级智能协同图库</div>
        <a-alert
          v-if="showWelcomeAlert"
          class="welcome-alert"
          message="欢迎使用触图万象图库系统"
          description="您可以使用页面底部的测试账号登录，或点击上方'点击选择账号'按钮快速体验系统功能。"
          type="info"
          show-icon
          closable
          @close="showWelcomeAlert = false"
        />
      </div>

      <div class="quick-login-section">
        <div class="quick-login-header">快速体验入口</div>
        <a-dropdown :trigger="['click']">
          <a-button
            class="quick-login-button"
            :loading="quickLoginLoading"
            size="large"
            type="primary"
          >
            <ThunderboltOutlined /> 点击选择账号 <DownOutlined />
          </a-button>
          <template #overlay>
            <a-menu @click="handleQuickLoginMenu">
              <a-menu-item key="zhangsan">张三账号</a-menu-item>
              <a-menu-item key="lisi">李四账号</a-menu-item>
              <a-menu-item key="wangwu">王五账号</a-menu-item>
              <a-menu-item key="zhaoliu">赵六账号</a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
        <div class="quick-login-tip">选择账号后将自动填充并登录 (密码：12345678)</div>
      </div>

      <a-form
        :model="formState"
        name="normal_login"
        class="login-form"
        @finish="submitForm"
        :validate-trigger="['change', 'blur']"
      >
        <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入用户名!' }]">
          <a-input
            v-model:value="formState.userAccount"
            allow-clear
            placeholder="默认用户名：zhangsan"
            size="large"
          >
            <template #prefix>
              <UserOutlined class="site-form-item-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item name="userPassword" :rules="[{ required: true, message: '请输入密码！' }]">
          <a-input-password
            v-model:value="formState.userPassword"
            allow-clear
            placeholder="默认密码：12345678"
            size="large"
          >
            <template #prefix>
              <LockOutlined class="site-form-item-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <div class="login-options">
            <a-form-item name="remember" no-style>
              <a-checkbox>记住我</a-checkbox>
            </a-form-item>
            <a class="login-form-forgot" href="">忘记密码</a>
          </div>
          <div class="login-methods">
            <a-tooltip>
              <template #title>暂未开放</template>
              <a-button type="text" @click="router.push('/user/login/sms')">
                <MobileOutlined /> 手机号登录
              </a-button>
            </a-tooltip>
            <a-tooltip>
              <template #title>推荐使用</template>
              <a-button type="text" @click="router.push('/user/login/email')">
                <MailOutlined /> 邮箱登录
              </a-button>
            </a-tooltip>
            <a-tooltip>
              <template #title>暂未开放</template>
              <a-button type="text" @click="router.push('/user/register')">
                <UserAddOutlined /> 立即注册
              </a-button>
            </a-tooltip>
          </div>
        </a-form-item>

        <a-form-item>
          <a-button
            :loading="loading"
            :disabled="disabledSubmit"
            type="primary"
            html-type="submit"
            class="login-form-button"
            size="large"
          >
            {{ loading ? '登录中...' : '登录' }}
          </a-button>
        </a-form-item>
      </a-form>

      <div class="login-help">
        <a-typography>
          <a-typography-paragraph>
            <a-tag :icon="h(UserOutlined)" color="blue">可用账号:</a-tag>
            <a-tag>zhangsan</a-tag>
            <a-tag>lisi</a-tag>
            <a-tag>wangwu</a-tag>
            <a-tag>zhaoliu</a-tag>
          </a-typography-paragraph>
          <a-typography-paragraph>
            <a-tag color="red">默认密码: 12345678</a-tag>
            <a href="" class="help-link">帮助中心</a>
          </a-typography-paragraph>
        </a-typography>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, h, reactive, ref } from 'vue'
import {
  LockOutlined,
  UserOutlined,
  MobileOutlined,
  MailOutlined,
  UserAddOutlined,
  DownOutlined,
  ThunderboltOutlined,
} from '@ant-design/icons-vue'
import { userLoginUsingPost } from '@/api/userController.ts'
import { useUserStore } from '@/stores/user.ts'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'

const formState = reactive({
  userAccount: '',
  userPassword: '',
})

const loading = ref(false)
const quickLoginLoading = ref(false)
const showWelcomeAlert = ref(true)

const router = useRouter()
const userStore = useUserStore()

// 快捷登录功能
const quickLogin = async (username = 'zhangsan') => {
  quickLoginLoading.value = true
  try {
    // 设置默认登录信息
    formState.userAccount = username
    formState.userPassword = '12345678'

    // 自动提交表单
    setTimeout(async () => {
      try {
        await submitForm()
      } catch (err) {
        console.error('快捷登录过程中出错:', err)
        message.error('快捷登录失败，请手动登录')
      } finally {
        quickLoginLoading.value = false
      }
    }, 500)
  } catch (err) {
    console.error('快捷登录失败:', err)
    message.error('快捷登录失败，请重试')
    quickLoginLoading.value = false
  }
}

// 处理菜单点击事件
const handleQuickLoginMenu = ({ key }: { key: string }) => {
  quickLogin(key)
}

const submitForm = async () => {
  if (!formState.userAccount || !formState.userPassword) {
    message.error('请填写完整的登录信息')
    return
  }

  loading.value = true
  try {
    const res = await userLoginUsingPost({
      userAccount: formState.userAccount,
      userPassword: formState.userPassword,
    })

    if (res.data.code === 0) {
      // 获取用户信息
      await userStore.fetchUserInfo()
      message.success('登录成功')
      // 跳转到首页
      await router.replace({
        path: '/',
      })
      // 刷新页面
      router.go(0)
    } else {
      message.error(res.data.message || '登录失败')
    }
  } catch (err) {
    console.error('登录失败:', err)
    message.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}

const disabledSubmit = computed(() => {
  return !(formState.userAccount && formState.userPassword)
})
</script>

<style scoped lang="scss">
#user-login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(24, 144, 255, 0.1) 0%, rgba(24, 144, 255, 0) 70%);
    top: -100px;
    left: -100px;
    z-index: 0;
  }

  &::after {
    content: '';
    position: absolute;
    width: 400px;
    height: 400px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(24, 144, 255, 0.1) 0%, rgba(24, 144, 255, 0) 70%);
    bottom: -150px;
    right: -150px;
    z-index: 0;
  }
}

.login-container {
  width: 100%;
  max-width: 420px;
  padding: 40px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 1;
  animation: fadeIn 0.5s ease-out;

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(20px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
}

.login-header {
  text-align: center;
  margin-bottom: 20px;

  h1 {
    font-size: 32px;
    color: #1890ff;
    margin-bottom: 8px;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(24, 144, 255, 0.2);
  }

  .title {
    font-size: 16px;
    color: #666;
    margin-bottom: 16px;
  }

  .welcome-alert {
    margin-top: 16px;
    text-align: left;
  }
}

.quick-login-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 24px;
  padding: 16px;
  background-color: #f0f7ff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.1);
  transition: all 0.3s ease;
  border: 1px solid rgba(24, 144, 255, 0.1);

  &:hover {
    box-shadow: 0 4px 12px rgba(24, 144, 255, 0.2);
    transform: translateY(-2px);
  }

  .quick-login-header {
    font-size: 16px;
    font-weight: 500;
    color: #1890ff;
    margin-bottom: 12px;
  }

  .quick-login-button {
    height: 40px;
    font-size: 16px;
    width: 80%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 8px;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    animation: pulse 2s infinite;

    @keyframes pulse {
      0% {
        box-shadow: 0 0 0 0 rgba(24, 144, 255, 0.4);
      }
      70% {
        box-shadow: 0 0 0 10px rgba(24, 144, 255, 0);
      }
      100% {
        box-shadow: 0 0 0 0 rgba(24, 144, 255, 0);
      }
    }

    &::after {
      content: '';
      position: absolute;
      top: -50%;
      left: -50%;
      width: 200%;
      height: 200%;
      background: rgba(255, 255, 255, 0.2);
      transform: rotate(30deg);
      transition: transform 0.6s;
      opacity: 0;
    }

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
      animation: none;

      &::after {
        transform: rotate(30deg) translate(10%, 10%);
        opacity: 1;
      }
    }
  }

  .quick-login-tip {
    font-size: 13px;
    color: #888;
    margin-top: 4px;
  }
}

.login-form {
  :deep(.ant-form-item) {
    margin-bottom: 24px;
  }

  :deep(.ant-input-affix-wrapper) {
    padding: 8px 11px;
  }

  :deep(.site-form-item-icon) {
    color: #bfbfbf;
  }
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.login-methods {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-bottom: 24px;

  :deep(.ant-btn) {
    display: flex;
    align-items: center;
    gap: 4px;
  }
}

.login-form-button {
  height: 40px;
  font-size: 16px;
  width: 100%;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 4px 8px rgba(24, 144, 255, 0.2);
    transform: translateY(-1px);
  }
}

.login-help {
  margin-top: 24px;
  text-align: center;

  :deep(.ant-tag) {
    margin: 4px;
    padding: 4px 8px;
  }

  .help-link {
    margin-left: 8px;
    color: #1890ff;
  }
}

:deep(.ant-form-item-explain-error) {
  font-size: 13px;
}
</style>
